﻿using Entity;
using IRealize;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using Realize;
using System.Threading.Tasks;
using WebApplication9.Controllers.ClientSet;

namespace WebApplication9.Controllers.Comp_Stand_m
{
    public class SS_ServerController : Controller
    {
        private readonly ILogger<SS_ServerController> _logger;
        private readonly IConfiguration configuration;

        public SS_ServerController(ILogger<SS_ServerController> logger, IConfiguration configuration)
        {
            this._logger = logger;
            this.configuration = configuration;
        }
        public IActionResult Index()
        {
            return View();
        }
        public async Task<IActionResult> Index2(int i,int p)
        {
            ISS ss = new SS();
            List<Salary_Standard> zhi = await ss.Server(configuration["SqlServer"]);
            List<Salary_Standard> zhi2 = zhi.Skip((i - 1) * p).Take(p).ToList();
            PagedViewModel<Salary_Standard> cc = new PagedViewModel<Salary_Standard>() { itemas = zhi2, total = zhi.Count };
            return Ok(new 
            {
                list =cc
            });
        }
        public async Task<IActionResult> Update(string sid) 
        {

            ISS ss = new SS();
            List<Salary_Standard> list = await ss.Server2(sid, configuration["SqlServer"]);
            Salary_Standard salary_Standard = list[0];

            ISSD ssd = new SSD();
            List<Salary_Standard_Details> lisr2 = await ssd.Server3(sid, configuration["SqlServer"]);
            if (lisr2.Count > 0)
            {
                ViewBag.list = lisr2;
                ViewBag.list2 = "aaa";
                
            }
            else 
            {
                ViewBag.list2 = "bbb";
            }
            
            return View(salary_Standard);
        }
        public class TransformedItem
        {
            public int id { get; set; }
            public int name { get; set; }
        }
        [HttpPost]
        public async Task<ActionResult> Update(string Dig, string XC_name, string ZE, string ZDR, string DJR, DateTime DJSJ, string BZ, string extraItems)
        {
            try
            {
                // 解析JSON字符串为对象列表
                var originalObject = JObject.Parse(extraItems);

                // 创建转换后的对象列表
                List<TransformedItem> transformedItems = new List<TransformedItem>();

                // 遍历原始对象的所有属性
                foreach (var property in originalObject.Properties())
                {
                    // 将属性名(键)转换为id，属性值转换为name
                    if (int.TryParse(property.Name, out int id) && int.TryParse(property.Value.ToString(), out int name))
                    {
                        transformedItems.Add(new TransformedItem { id = id, name = name });
                    }
                }
                ISSD ssd = new SSD();
                List<int> list = new List<int>();
                ISS ss = new SS();
                int i = await ss.Update(Dig, XC_name, double.Parse(ZE), ZDR, DJR, DJSJ, BZ, configuration["SqlServer"]);
                if (i > 0)
                {
                    list.Add(i);
                    foreach (var item in transformedItems)
                    {
                        int i2 = await ssd.Update2(item.id, item.name, configuration["SqlServer"]);
                        if (i2 > 0)
                        {
                            list.Add(i2);
                        }
                    }


                }
                int i3 = 0;
                for (int p = 0; p < list.Count; p++)
                {
                    if (list[p] > 0)
                    {
                        i3++;
                    }
                }
                if (i3 == list.Count)
                {
                    return Ok(new
                    {
                        fh = 1
                    });
                }
                return Ok(new
                {
                    fh = 0
                });
                // 在控制台输出接收到的数据（实际项目中可使用日志记录）

            }
            catch (Exception ex)
            {
                Console.WriteLine($"解析extraItems出错: {ex.Message}");
                return BadRequest(new { fh = 0 }); // 操作失败
            }
        }
    }
}
